<template>
 <div id="signin">
    <div class="logo">
      <img alt="logo" src="../assets/logo.png">
    </div>
    <div class="signin">
      <el-form :model="form" status-icon :rules="rules" ref="form" size="mini">
        <el-form-item label="通行证" prop="account">
          <el-input v-model="form.account"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="form.checkPass"></el-input>
        </el-form-item>
        <el-form-item label="邀请码" prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="signUp('form')">SignUp</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    var checkCode = (rule, value, callback) => {
      if (!value || value === '') {
        return callback(new Error('邀请码不能为空'));
      }
      callback();
    };
    var checkAccount = (rule, value, callback) => {
      if (!value || value === '') {
        return callback(new Error('通行证不能为空'));
      }
      callback();
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.form.checkPass !== '') {
          this.$refs.form.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      form: {
        account: "",
        password: "",
        checkPass: "",
        code: ""
      },
      rules: {
        account: [
          { validator: checkAccount, trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        code: [
          { validator: checkCode, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    signUp(formName) {
      let _self = this;
      _self.$refs[formName].validate((valid) => {
        if (valid) {
            _self.axios.post(api.signup, _self.form).then((resp) => {
              if(200 == resp.data.code) {
                _self.$router.push("/")
                _self.$message.success(resp.data.msg)
              } else {
                _self.$message.warning(resp.data.msg)
              }
            },(resp) => {
              _self.$message.error("Internal Server Error")
            })
        } else {
          return false;
        }
      });
    },
  }
}
</script>
<style scoped>
#signin {
  text-align: center;
  color: #2c3e50;
  background-color: #C0C4CC;
  height: 100%;
}

.signin {
  margin-left: 50%;
  transform: translateX(-50%) translateY(60px);
  width: 380px;
}
.logo {
  transform: translateY(60px);
}

</style>
    